<template>
	<view class="container">

		<!-- 头像选择区域 -->
		<view class="avatar-select-container">
			<!-- 顶部操作按钮 -->
			<view class="action-buttons">
				<text class="cancel-btn" @click="goBack">取消</text>
				<text class="confirm-btn" @click="confirmSelection">确认</text>
			</view>

			<!-- 头像选择网格 -->
			<view class="avatar-grid">
				<view 
					class="avatar-item" 
					v-for="(avatar, index) in avatarList" 
					:key="index"
					@click="selectAvatar(index)"
				>
					<image :src="avatar.url" class="avatar-img" mode="aspectFill"></image>
					<view class="select-indicator" v-if="selectedIndex === index">
						<text class="check-icon">✓</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedIndex: -1, // 当前选中的头像索引
				// 预设头像列表
				avatarList: [
					{
						id: 1,
						url: '/static/avatar/avatar1.png',
						name: '棕色小熊'
					},
					{
						id: 2,
						url: '/static/avatar/avatar2.png',
						name: '戴眼镜的人'
					},
					{
						id: 3,
						url: '/static/avatar/avatar3.png',
						name: '棕色小熊2'
					},
					{
						id: 4,
						url: '/static/avatar/avatar4.png',
						name: '戴眼镜的人2'
					},
					{
						id: 5,
						url: '/static/avatar/upload.png',
						name: '上传照片'
					}
				]
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			// 选择头像
			selectAvatar(index) {
				if (index === 4) {
					// 点击上传照片
					this.uploadCustomAvatar();
				} else {
					this.selectedIndex = index;
				}
			},
			// 上传自定义头像
			uploadCustomAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						const tempFilePath = res.tempFilePaths[0];
						// 将上传的图片添加到头像列表
						this.avatarList[4] = {
							id: 5,
							url: tempFilePath,
							name: '自定义头像'
						};
						this.selectedIndex = 4;
						uni.showToast({
							title: '头像上传成功',
							icon: 'success'
						});
					},
					fail: (err) => {
						console.error('选择图片失败:', err);
						uni.showToast({
							title: '选择图片失败',
							icon: 'none'
						});
					}
				});
			},
			// 确认选择
			confirmSelection() {
				if (this.selectedIndex === -1) {
					uni.showToast({
						title: '请选择一个头像',
						icon: 'none'
					});
					return;
				}

				const selectedAvatar = this.avatarList[this.selectedIndex];
				
				// 保存选中的头像到本地存储
				const userInfo = uni.getStorageSync('userInfo') || {};
				userInfo.avatar = selectedAvatar.url;
				uni.setStorageSync('userInfo', userInfo);

				uni.showToast({
					title: '头像设置成功',
					icon: 'success'
				});

				// 延迟返回，让用户看到成功提示
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					});
				}, 1500);
			}
		},
		onLoad() {
			// 页面加载时获取当前头像
			this.getCurrentAvatar();
		},
		// 获取当前头像
		getCurrentAvatar() {
			const userInfo = uni.getStorageSync('userInfo');
			if (userInfo && userInfo.avatar) {
				// 查找当前头像在列表中的位置
				const currentIndex = this.avatarList.findIndex(avatar => avatar.url === userInfo.avatar);
				if (currentIndex !== -1) {
					this.selectedIndex = currentIndex;
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #f5f7fa;
		min-height: 100vh;
	}

	/* 顶部导航栏 */
	.header {
		background: linear-gradient(135deg, #3183FB 0%, #3183FB 100%);
		padding-top: var(--status-bar-height);
	}

	.nav-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 30rpx 30rpx;
		height: 88rpx;
	}

	.nav-left {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		font-size: 48rpx;
		color: white;
		font-weight: bold;
	}

	.nav-title {
		color: white;
		font-size: 36rpx;
		font-weight: bold;
	}

	.nav-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
		width: 60rpx;
		height: 60rpx;
		justify-content: center;
	}

	.more-icon {
		font-size: 32rpx;
		color: white;
	}

	.circle-icon {
		font-size: 24rpx;
		color: white;
	}

	/* 头像选择容器 */
	.avatar-select-container {
		background-color: white;
		margin: 20rpx;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	/* 操作按钮 */
	.action-buttons {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.cancel-btn,
	.confirm-btn {
		font-size: 32rpx;
		color: #333;
		padding: 10rpx 20rpx;
	}

	.confirm-btn {
		color: #3183FB;
		font-weight: 500;
	}

	/* 头像网格 */
	.avatar-grid {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 30rpx;
		padding: 30rpx;
	}

	.avatar-item {
		position: relative;
		width: 100%;
		aspect-ratio: 1;
		border-radius: 15rpx;
		overflow: hidden;
		border: 2rpx solid #f0f0f0;
	}

	.avatar-img {
		width: 100%;
		height: 100%;
	}

	/* 选择指示器 */
	.select-indicator {
		position: absolute;
		top: 10rpx;
		right: 10rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: #3183FB;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 2rpx solid white;
	}

	.check-icon {
		color: white;
		font-size: 24rpx;
		font-weight: bold;
	}

	/* 上传照片特殊样式 */
	.avatar-item:last-child {
		background-color: #f8f9fa;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		border: 2rpx dashed #ccc;
	}

	.avatar-item:last-child .avatar-img {
		width: 60rpx;
		height: 60rpx;
		opacity: 0.5;
	}

	.avatar-item:last-child::after {
		content: '上传照片';
		position: absolute;
		bottom: 20rpx;
		font-size: 24rpx;
		color: #999;
	}
</style>